**substantive issues over time

use HKCFA_u.dta, clear 


* Step 1: Create a dataset with unique values and frequencies
preserve
contract issue, freq(count)

summarize count
local total_count = r(sum)

* Step 2: Sort by frequency in descending order
gsort -count

* Step 3: Generate percentage for each category
gen percentage = (count / `total_count') * 100

* Step 3: Open a .tex file and write the LaTeX table headers
capture file close mytable
file open mytable using "output_table.tex", write text replace
file write mytable "\begin{table}[ht]\centering \begin{tabular}{lcc} \hline"
file write mytable "Issue & Frequency & percentage \\ \hline"

* Step 4: Loop through the sorted data and write to the .tex file
forvalues i = 1/`=_N' {
    local issue_name = issue[`i']
    local freq = count[`i']
	local pct = percentage[`i']
    file write mytable "`issue_name' & `freq' & `=round(`pct', 0.1)'\% \\"
}

* Step 6: Write the total count as the last row
file write mytable "Total & `total_count' & 100\% \\"

* Step 5: Close the table and the file
file write mytable "\hline \end{tabular} \caption{Frequency of Issues} \end{table}"
file close mytable

* Restore the original dataset
restore
